package com.example.javaee.service.impl;

import com.example.javaee.entity.User;
import com.example.javaee.mapper.UserMapper;
import com.example.javaee.service.UserService;
import com.example.javaee.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int register(User user) {
        // 检查用户名是否已存在
        int existUser = userMapper.findUserByName(user.get管理员账户());
        if (existUser != 0) {
            throw new BusinessException("用户名已存在");
        }
        //如果数据库中不存在这个用户名，就添加到数据库中去
        return userMapper.creatUser(user);
    }

    @Override
    public boolean login(String username, String password) {
        // 查询用户
        User user = userMapper.getUserByName(username, password);
        if(user == null){
            log.info("账号密码不存在");
            return false;
        }
        log.info("登录成功");
        return true;
    }
}


